package org.codeart.api_driver.service;

import org.codeart.api_driver.remote.ServiceDriverUserClient;
import org.codeart.api_driver.remote.ServiceMapClient;
import org.codeart.common.dto.Car;
import org.codeart.common.request.DriverPointRequest;
import org.codeart.common.request.PointRequest;
import org.codeart.common.util.HttpStatus;
import org.codeart.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PointService {

    @Autowired
    private ServiceDriverUserClient driverUserClient;

    @Autowired
    private ServiceMapClient serviceMapClient;

    public boolean upload(DriverPointRequest driverPointRequest) {
        // 获取carId
        Long carId = driverPointRequest.getCarId();

        // 通过carId获取tid trid，调用service-driver-user
        R<Car> r = driverUserClient.getCarById(carId);
        Car car = r.getData();
        if (car == null) {
            return false;
        }
        Integer tid = car.getTid();
        Integer trid = car.getTrid();

        // 通过地图服务上传
        PointRequest pointRequest = new PointRequest();
        pointRequest.setTid(tid).setTrid(trid).setPoints(driverPointRequest.getPoints());
        R<Void> r1 = serviceMapClient.upload(pointRequest);
        return r1.getCode() == HttpStatus.HTTP_OK;
    }

}
